<?php
namespace Forms\Validator;

use Pinenut\Form\Validator;

class LengthAction extends Validator
{
	protected $defaultErrorMessage = 'Undefinded length error';

	protected function isValid($value, $params)
	{
		$value = (string)$value;
		$length = mb_strlen($value, 'utf8');
		if (!empty($params['min']) && ($length < $params['min'])) {
			$this->defaultErrorMessage = 'Too short';
			return false;
		}
		if (!empty($params['max']) && ($length > $params['max'])) {
			$this->defaultErrorMessage = 'Too long';
			return false;
		}

		return true;
	}
}